<?php

tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');

class BannerAction extends AdministratorAction {

	public function _initialize(){
		$this->pageTitle['index'] = '展示中广告';
		$this->pageTitle['noshow'] = '未展示广告';
		parent::_initialize();
	}
	//广告列表
	public function index() {
		$_REQUEST['tabHash'] = 'index';
		// 初始化用户列表管理菜单
		$this->_initBannerListAdminMenu("index");
		// 数据的格式化与listKey保持一致
		$map["status"] = 1;
		$listData = $this->_getBannerList('20', $map,"index");
		
		// 列表批量操作按钮
		$this->pageButton[] = array('title'=>"搜索广告",'onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>"批量取消展示",'onclick'=>"admin.offBanners('')");
		//$this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_USER_GROUP'),'onclick'=>"admin.changeUserGroup()");
		// 转移用户部门，如果需要请将下面的注释打开
		// $this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_DEPARTMENT'),'onclick'=>"admin.changeUserDepartment()");
		$this->displayList($listData);
		
	}
	public function noshow(){
		$_REQUEST['tabHash'] = 'noshow';
		// 初始化用户列表管理菜单
		$this->_initBannerListAdminMenu("noshow");
		// 数据的格式化与listKey保持一致
		$map["status"] = 0;
		$listData = $this->_getBannerList('20', $map,"noshow");
		
		// 列表批量操作按钮
		$this->pageButton[] = array('title'=>"搜索广告",'onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>"批量删除",'onclick'=>"admin.delBanners('')");
		//$this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_USER_GROUP'),'onclick'=>"admin.changeUserGroup()");
		// 转移用户部门，如果需要请将下面的注释打开
		// $this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_DEPARTMENT'),'onclick'=>"admin.changeUserDepartment()");
		$this->displayList($listData);
	}
	
	/**
	 * 初始化Banner列表管理菜单
	 * @param string $type 列表类型，index、noshow
	 */
	private function _initBannerListAdminMenu() {
		// tab选项
		$this->pageTab[] = array('title'=>"展示中",'tabHash'=>'index','url'=>U('admin/Banner/index'));
		$this->pageTab[] = array('title'=>"未展示",'tabHash'=>'noshow','url'=>U('admin/Banner/noshow'));
		
		// 搜索选项的key值
		// $this->searchKey = array('uid','uname','email','sex','department','user_group',array('ctime','ctime1'));
		$this->searchKey = array('id','title','status',array('ctime','ctime1'));
		// 针对搜索的特殊选项
		$this->opt['status'] = array('0'=>"未展示",'1'=>"展示中");
		
		
		$map['pid'] = array('NEQ', 0);
	
		// 列表key值 DOACTION表示操作
		$this->pageKeyList = array('id','title','status','image','url','ctime','DOACTION');
	
		/*		if(!empty($_POST['_parent_dept_id'])) {
		 $this->onload[] = "admin.departDefault('".implode(',', $_POST['_parent_dept_id'])."','form_user_department')";
		}*/
	}
	
	/**
	 * 解析用户列表数据
	 * @param integer $limit 结果集数目，默认为20
	 * @param array $map 查询条件
	 * @param string $type 格式化数据类型，index、pending、dellist
	 * @return array 解析后的用户列表数据
	 */
	private function _getBannerList($limit = 20, $map = array(),$tab) {
		// 设置列表主键
		$this->_listpk = 'id';
		// 取用户列表
		
		$listData = model('Banner')->getBannerList($limit, $map);
		//dump($listData);exit;
		foreach($listData['data'] as $k => $v) {
			//获取用户组信息
			
			
			$image = D('attach')->where('attach_id='.$listData['data'][$k]['image'])->find();
			$image_url = $image['save_path'].$image['save_name'];
			$listData['data'][$k]['image'] = getImageUrl($image_url);
			
			$listData['data'][$k]['image'] = "<img style='height:100px;' src='".$listData['data'][$k]['image']."'"." />";
			$listData['data'][$k]['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
			if ($listData['data'][$k]['status'] == 0){
				$listData['data'][$k]['status'] = "未展示";
			}elseif ($listData['data'][$k]['status'] ==1){
				$listData['data'][$k]['status'] = "展示中";
			}
			
			//设置操作
			
			$listData['data'][$k]['DOACTION'] = '<a href="'.U('admin/Banner/editBanner',array('tabHash'=>'editUser','id'=>$v['id'])).'">'.L('PUBLIC_EDIT').'</a> - ';
			$listData['data'][$k]['DOACTION'] .= $v['status'] == 1 ? '<a href="javascript:void(0)" onclick="admin.offBanner(\''.$v['id'].'\')">'.'取消展示'.'</a> - ' : '<a href="javascript:void(0)" onclick="admin.showBanner(\''.$v['id'].'\')">'.'展示'.'</a> - ';
			$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.delBanner(\''.$v['id'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
		}
		return $listData;
	}
	
	public function doDeleteBanner(){
		if(empty($_POST['id'])) {
			$return['status'] = 0;
			$return['data'] = '';
			exit(json_encode("没有数据"));
		}

			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = 1;
			$return['data'] = $_REQUEST;			// 操作成功
		
		exit(json_encode($return));
	}
	//编辑广告
	public function editBanner(){
		$this->assign('pageTitle','编辑广告');
		
		$this->pageKeyList = array('image','title','url','status');
		$this->opt['status'] = array('1'=>'是','0'=>'否');
		//$this->opt['essence'] = array('1'=>'是','0'=>'否');
		$banner = model('Banner')->where('id='.$_GET['id'])->find();
		if($banner['image']){
			$image = D('attach')->where('attach_id='.$banner['image'])->find();
			$image_url = $image['save_path'].$image['save_name'];
			$banner['image_url'] = getImageUrl($image_url);
		}
		$this->notEmpty = array('image');
		$this->savePostUrl = U('admin/Banner/doEditBanner',array("id"=>$_GET['id']));
		$this->onsubmit = 'admin.BannerCheck(this)';
		$this->displayConfig($banner);
	}
	
	public function doEditBanner(){
		$banner = M("Banner");
		$id = t($_REQUEST['id']);
		$banner->image = t($_REQUEST["image"]);
		$banner->title =t( $_REQUEST["title"]);
		$banner->url = t($_REQUEST["url"]);
		$banner->status = t($_REQUEST["status"]);
		
		if($banner->where("id = ".$id)->save()){
			$this->assign('jumpUrl', U('admin/Banner/index'));
			$this->success(L('PUBLIC_SYSTEM_MODIFY_SUCCESS'));
		}else{
			$this->error($banner->getError());
		}
	}
	//取消展示
	public function doOffBanner(){
		$banner = M("Banner");
		$id = $_REQUEST['id'];
		$banner->status = 0;
		if($banner->where("id = $id")->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
	
		exit(json_encode($return));
	}
	
	public function doOffBanners(){
		$banner = M("Banner");
		$id = $_REQUEST['id'];
		$str = "id in (";
		for($i=0;$i<count($id);$i++){
			$str .= $id[$i].',';
		}
		$str = trim($str,',');
		$str .= ")";
		$banner->status = 0;
		if($banner->where($str)->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
		
		exit(json_encode($return));
	}
	//展示
	public function doShowBanner(){
		$banner = M("Banner");
	
		$id = $_REQUEST['id'];
		
		$banner->status = 1;
		if($banner->where("id = $id")->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
		
		exit(json_encode($return));
		
	}	
	
	//添加
	public function addBanner() {
		$this->assign('pageTitle','增加广告');
		// 初始化用户列表管理菜单
		
	
		$this->pageKeyList = array("image","url","title","status");
	
	
		// 字段选项配置
		$this->opt['status'] = array('0'=>"不展示",'1'=>"展示");
	
		// 表单URL设置
		$this->savePostUrl = U('admin/Banner/doAddBanner');
		$this->notEmpty = array("image","status");
		$this->onsubmit = 'admin.addBannerSubmitCheck(this)';
	
		$this->displayConfig();
	}
	public function doAddBanner() {
	
		if(h(t($_POST['url'])) != ""){
			$res = preg_match('/^(?:https?|ftp):\/\/(?:www\.)?(?:[a-zA-Z0-9][a-zA-Z0-9\-]*)/',h($_POST['url']));
			if(!$res) $this->error('外链格式错误');
		}
		$banner = M('Banner');
		$map = $banner->create();
		$map["ctime"] = time();
		// 检查map返回值，有表单验证
		$result = $banner->add($map);
		if($result) {
			$this->assign('jumpUrl', U('admin/Banner/index'));
			$this->success(L('PUBLIC_ADD_SUCCESS'));
		} else {
			$this->error($banner->getError());
		}
	}
	
	//删除
	public function dodelBanner(){
		$banner = M("Banner");
		$id = $_REQUEST['id'];
		
		if($banner->where("id = $id")->delete()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
	
		exit(json_encode($return));
	}
	public function dodelBanners(){
		$banner = M("Banner");
		$id = $_REQUEST['id'];
		$str = "id in (";
		for($i=0;$i<count($id);$i++){
			$str .= $id[$i].',';
		}
		$str = trim($str,',');
		$str .= ")";
		
		if($banner->where($str)->delete()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
		
		exit(json_encode($return));
	}
}
